﻿using Mes.Helper;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.SessionState;

namespace SaicMotor
{
    /// <summary>
    /// SaciMotor 的摘要说明
    /// </summary>
    public class SaciMotor : IHttpHandler, IRequiresSessionState
    {
        private readonly static string conn = ConfigurationManager.ConnectionStrings["AppConnStr"].ConnectionString.ToString();

        #region 通用调用查询

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            switch (context.Request["action"])
            {
                //查界面主数据
                case "GetHtmlInfo":
                    GetHtmlInfo(context);
                    break;
                //查界面其他数据
                case "GetOtherInfo":
                    GetOtherInfo(context);
                    break;
                //查标题
                case "GetTittleInfo":
                    GetTittleInfo(context);
                    break;
            }
        }

        #endregion 通用调用查询

        #region 查界面主数据

        private void GetHtmlInfo(HttpContext context)
        {
            string json = "";

            DataSet ds = new DataSet();

            string sql = @"SELECT TOP 2 [Id]
                                      ,[PartType]
                                      ,[PartName]
                                      ,[EdiNum]
                                      ,[ToBeSort]
                                      ,[ToBeDelivered]
                                      ,[InTransit]
                                      ,[LineSideNum]
                                  FROM [MES.Board].[dbo].[T_Basic_KanBanInfo]";
            ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql);
            json = DataToJson(ds.Tables[0]);

            context.Response.Write(json);
        }

        #endregion 查界面主数据

        #region 查界面其他数据

        private void GetOtherInfo(HttpContext context)
        {
            string json = "";

            DataSet ds = new DataSet();
            string sql = @"SELECT TOP 1    [Id]
                                          ,[Title]
                                          ,[Slogan]
                                      FROM [MES.Board].[dbo].[T_Basic_OtherInfo]";
            ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql);
            json = DataToJson(ds.Tables[0]);

            context.Response.Write(json);
        }

        #endregion 查界面其他数据

        #region 查标题

        private void GetTittleInfo(HttpContext context)
        {
            string json = "";

            DataSet ds = new DataSet();
            string sql = @"SELECT TOP 1 [Id]
                                      ,[c1]
                                      ,[c2]
                                      ,[c3]
                                      ,[c4]
                                      ,[c5]
                                      ,[c6]
                                      ,[c7]
                                  FROM [MES.Board].[dbo].[T_Basic_Tittle]";
            ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql);
            json = DataToJson(ds.Tables[0]);

            context.Response.Write(json);
        }

        #endregion 查标题

        #region DataTable转Json

        private string DataToJson(DataTable dt)
        {
            System.Text.StringBuilder jsonBuilder = new System.Text.StringBuilder();
            jsonBuilder.Append("{\"Name\":\"" + dt.TableName + "\",\"Rows");
            jsonBuilder.Append("\":[");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                jsonBuilder.Append("{");
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    jsonBuilder.Append("\"");
                    jsonBuilder.Append(dt.Columns[j].ColumnName);
                    jsonBuilder.Append("\":\"");
                    jsonBuilder.Append(dt.Rows[i][j].ToString().Trim().Replace("\"", "\\\"").Replace("\n", "").Replace("\r", "")); //对于特殊字符，还应该进行特别的处理。
                    jsonBuilder.Append("\",");
                }
                jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
                jsonBuilder.Append("},");
            }
            jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
            jsonBuilder.Append("]");
            jsonBuilder.Append("}");
            return jsonBuilder.ToString();
        }

        #endregion DataTable转Json
    }
}